import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Fase extends JPanel {
	Submarino submarino;
	Thread t;

	public Fase() {
		submarino = new Submarino();
		JLabel label = new JLabel(new ImageIcon(getClass().getResource("bg.png")));
		add(label);


		add(submarino);
		submarino.setBounds(200,200, 100, 66);

		t = new Thread(new Runnable() {

			@Override
			public void run() {
				while(true) 
				{
					if(submarino.parado) {
						for(int i = 0; i < 4; i++)
						{
							submarino.frame = i;
							repaint();
							try {
								Thread.sleep(100);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					} else if(submarino.mudar) {

						if(submarino.direcao.equals("esquerda")) {
							for(int i = 14; i >= 9; i--) {
								submarino.frame = i;
								try {
									Thread.sleep(100);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}


						submarino.parado = true;
						submarino.mudar = false;
					}
				}
			}
		});
		t.start();

	}
	public void desenharQuadro(Image source, Graphics g,
			int x, int y, int cols, int frame,
			int width, int height)
	{
		int fx = (frame % cols) * width;
		int fy = (frame / cols) * height;
		g.drawImage(source, x, y, x+width, y+height,
				fx, fy, fx+width, fy+height, this);


	}

	public void paint(Graphics g) {
		super.paint(g);
		//		desenharFase(fase, g);
		desenharQuadro(submarino.imgQuadros, g, submarino.x, submarino.y, 8, submarino.frame, 100, 66);

	}

}
